// pages/goods/detail/detail.js
var api = require('../../../utils/api.js')
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    point_index:0,
    point_id:'',
    id:'',
    api:api,
    content:null,
    path:api.path,
    active:0,
    color_ids:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.getData()
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  getData(){
    let params = {
      id:this.data.id,
      member_id:api.cache('user').member_id||""
    }
    api.post('diy_scene',params).then(res => {
      if (res.code == 200) {
        if(res.data.length==0){
          api.tip("该产品暂无相关信息")
        }
        this.setData({
          content:res.data,
          point_id:res.data.default_point_id
        })
        this.handleColors()
      }
    })
  },
  async handleColors(){
    //处理其他锚点的默认第一个款式id
    let ids = this.data.content.scene.diy_points.map(ele=>ele.id)
    //过滤掉当前锚点id
    let fids = ids.filter(ele=>ele!=this.data.point_id)
    let defaultId = this.data.content.default_color_arr[this.data.active].id
    let arr = [defaultId]
    for (let i = 0; i < fids.length; i++) {
      let id = await this.handlePointColors(fids[i])
      arr.push(id)
    }
    this.setData({
      color_ids:arr
    })
  },
  changeActive(){
    //切换锚点要去循环获取当前锚点选中的第几个样式
    let pid = this.data.color_ids[this.data.point_index]
    this.data.content.default_color_arr.forEach((ele,index)=>{
      if(ele.id == pid){
        this.setData({
          active:index
        })
      }
    })
  },
  changePoint(e){
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    this.setData({
      point_id:id,
      point_index:index
    })
    let params = {
      scene_id:this.data.id,
      point_id:id
    }
    api.post('point_default_color',params).then(res => {
      if (res.code == 200) {
        if(res.data.length==0){
          api.tip("该产品暂无可替换瓷砖信息")
        }
        let content = this.data.content
        content.default_color_arr = res.data.default_color_arr
        this.setData({
          content
        })
        this.changeActive()
      }
    })
  },
  changeScene(e){
    let index = e.currentTarget.dataset.index
    this.setData({
      active:index
    })
    this.data.color_ids[this.data.point_index] = this.data.content.default_color_arr[this.data.active].id
    this.setData({
      color_ids:this.data.color_ids
    })
  },
  async handlePointColors(id){
    return await new Promise((resolve)=>{
      let params = {
        scene_id:this.data.id,
        point_id:id
      }
      api.post('point_default_color',params).then(res => {
        if (res.code == 200) {
          resolve(res.data.default_color_arr[0].id)
        }
      })
    })
  },
  async render(){
    wx.showLoading({
      mask: true,
      title:"渲染中"
    })
    setTimeout(()=>{
      wx.hideLoading()
      this.putData()
    },2000)
  },
  putData(){
    let data = this.data
    let params = {
      scene_id:data.id,
      curr_point_id:data.point_id,
      color_ids:this.data.color_ids.join(",")
    }
    api.post('diy_render',params).then(res => {
      if (res.code == 200) {
        let content = this.data.content
        content.scene.big_thumb = res.data.show_thumb
          this.setData({
            content
          })
        //保存当前用户的渲染记录
        this.writeLog(res.data.id)
      }else{
        api.tip("换砖失败")
      }
    })
  },
  writeLog(id){
    let params = {
      member_id:api.cache('user').member_id||"",
      dd_id:id
    }
    api.post('write_diy_log',params).then(res => {
      if (res.code == 200) {
        console.log("成功保存当前用户的渲染记录")
      }else{
        console.log("保存失败")
      }
    })
  },
  operation(e){
    let user = api.cache("user")
    let res = api.checkLogin()
    if (!res) {
      return;
    }
    let index = e.currentTarget.dataset.index
    let url = ''
    if(index==1){
      if(this.data.content.is_fav==0){
        url = 'click_fav'
      }else{
        url = 'del_fav_single'
      }
    }else if(index==2){
      if(this.data.content.is_love==0){
        url = 'click_zan'
      }else{
        url = 'del_love_single'
      }
    }else if(index==3){
      url = 'click_share'
    }
    let params={
      id:this.data.content.scene.id,
      data_type:5,
      member_id:user.member_id,
    }
    api.post(url,params).then(res=>{
      if(res.code==200){
        let content = this.data.content
        if(index==1){
          if(this.data.content.is_fav==0){
            content.fav_total +=1
            content.is_fav = 1
            api.tip("成功收藏")
          }else{
            content.fav_total -=1
            content.is_fav = 0
            api.tip("取消收藏")
          }
        }else if(index==2){
          if(this.data.content.is_love==0){
            content.love_total +=1
            content.is_love = 1
            api.tip("成功点赞")
          }else{
            content.love_total -=1
            content.is_love = 0
            api.tip("取消点赞")
          }
        }else if(index==3){
          content.share_total +=1
        }
        this.setData({
          content
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})